package bs.owb.interceptors;

import javax.inject.Named;
import javax.inject.Singleton;

@Named("transactionManager")
@Singleton
public class SimpleTransactionManager {

    private boolean transactionInProgress = false;

    private int transactionCounter = 0;

    public boolean isTransactionInProgress() {
        return transactionInProgress;
    }

    public void startTransaction() {
        if (transactionInProgress) {
            throw new IllegalStateException("Transaction already in progess");
        } else {
            transactionInProgress = true;
            transactionCounter++;
        }
    }

    public void stopTransaction() {
        if (!transactionInProgress) {
            throw new IllegalStateException("No transaction in progress");
        } else {
            transactionInProgress = false;
        }
    }

    public int getTransactionCounter() {
        return transactionCounter;
    }
}
